package nl.saxion.ami;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.paar.ch3marker.R;

import edu.dhbw.andar.ARObject;
import edu.dhbw.andar.ARToolkit;
import edu.dhbw.andar.AndARActivity;
import edu.dhbw.andar.exceptions.AndARException;

@SuppressLint("UseSparseArrays")
public class Activity extends AndARActivity {

	private ARObject sensorWaarde;
	private ARToolkit artoolkit;
	private HashMap<Integer, String> sensoren;
	TextView tv;
	ChartView chartview;
	private boolean grafiek = false;
	private ArrayList<ARObject> sensorwaarden;
	private String url;
	private String url2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//onze layout toevoegen aan de andar layout
		LayoutInflater inflater = getLayoutInflater();
		getWindow().addContentView(inflater.inflate(R.layout.main, null), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT));

		// url voor laatste sensorwaarde
		url = "http://145.76.19.101:8080/RestletWebservice/ar/";
		
		//url voor waardenreeks (grafiek)
		url2 = "http://145.76.19.101:8080/RestletWebservice/ar2/";
		
		sensoren = new HashMap<Integer, String>();
		
		//Sensorenid's toevoegen met bijbehorende markers (marker uit database halen later)
		sensoren.put(1, "testmarker.pat");
		sensoren.put(4, "markerY.pat");
		sensoren.put(3, "markera.pat");
		sensoren.put(5, "markerB.pat");
		sensoren.put(2, "markerG.pat");
		
		sensoren.put(6, "markerj.pat");
		sensoren.put(7, "markerW.pat");
		sensoren.put(8, "marker1.pat");
		sensoren.put(9, "marker2.pat");
		sensoren.put(10, "marker3.pat");
		
		sensoren.put(11, "marker4.pat");
		

		CustomRenderer renderer = new CustomRenderer();
		setNonARRenderer(renderer);

		registerSensoren();
		startPreview();

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		//timers stopzetten van alle arobjecten
		for(ARObject aro : sensorwaarden){
			SensorWaarde sw = (SensorWaarde) aro;
			if(sw != null){
				sw.stopTimer();
			}
		}
	}


	public void registerSensoren(){
		try {
			artoolkit = getArtoolkit();
			sensorwaarden = new ArrayList<ARObject>();
			// alle sensoren met patterns registreren. 
			for (HashMap.Entry<Integer, String> entry : sensoren.entrySet()) {
				int sensorid = entry.getKey();
				String patternname = entry.getValue();
				sensorWaarde = new SensorWaarde
						("test", patternname, 160.0, new double[]{0,0}, sensorid, this);
				artoolkit.registerARObject(sensorWaarde);
				sensorwaarden.add(sensorWaarde);
			}
		} catch (AndARException ex){
			System.out.println("andarexecption");
		}		
	}



	// laatste waarde en naam (later eenheid) van sensor ophalen en in Sensor object zetten. 
	public Sensor getWaardeSensor(int sensorid) {
		ClientResource cr = new ClientResource(url + sensorid);
		cr.setRetryOnError(false);
		try {
			cr.get().write(System.out);
		} catch (ResourceException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		SensorResource resource = cr.wrap(SensorResource.class);

		String sensorjson = resource.retrieve();
		Gson gson = new Gson();
		Sensor sensor = gson.fromJson(sensorjson, Sensor.class);   
		return sensor;
	}

	// reeks van waarden ophalen voor grafiek. 
	public HashMap<Long, Double> getWaardenreeks(int sensorid) {
		         ClientResource cr = new ClientResource(url2 + sensorid);
		         
		         cr.setRetryOnError(false);
		 		
		         WaardeReeksResource resource = cr.wrap(WaardeReeksResource.class);
		
		         String waardenreeksjson = resource.retrieve();
		         System.out.println("json:: " + waardenreeksjson);
		         Gson gson = new Gson();
		         WaardenReeksLijst waarden =  gson.fromJson(waardenreeksjson, WaardenReeksLijst.class);
		         System.out.println(waarden.getWaarden().size());
		         return waarden.getWaarden();
	}

	// weergeven van de grafiek. 
	public void showGrafiek(View view) {
		System.out.println("button geklikt");
		//		System.out.println(view.getTag());

		if(grafiek){
			chartview.setVisibility(View.INVISIBLE);
			grafiek = !grafiek;
		}else{
			
			//TODO sensorid koppelen aan waarop geklikt wordt. 
			HashMap<Long, Double> waarden = this.getWaardenreeks(1);
			
			chartview = new ChartView(this, waarden);
			
			RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
			LayoutParams theViewLayoutParams = new LayoutParams(450,450);
			chartview.setLayoutParams(theViewLayoutParams);
			rl.addView(chartview);
			chartview.setVisibility(View.VISIBLE);
			grafiek = !grafiek;
		}

	}


	public void uncaughtException(Thread thread, Throwable ex) {
		Log.e("AndAR EXCEPTION", ex.getMessage());
		finish();
	}

	public ChartView getChartView() {
		return chartview;
	}

}
